<?php

namespace App\Http\Controllers;


use Cache;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Hash;
use Config;
use DB;

class WeiController extends Controller
{

    public function __construct()
    {
        session_start();
        $this->appid = Config('app.appid');
        $this->secret = Config('app.appsecret');
    }

    public function goHome()
    {
        $user_obj = $this->wei();
        $_SESSION['user'] = $user_obj;

        $nickname = $user_obj['nickname'];
        $openid = $user_obj['openid'];
        $faceimg = $user_obj['headimgurl'];
        $city = $user_obj['city'];
        $province = $user_obj['province'];
        $sex = $user_obj['sex'];

        $sql = "select id from ht_customer where openid='" . $user_obj['openid'] . "'";
        $inDB = DB::select($sql);

        if (count($inDB) > 0) {
            $sql = "update ht_customer set customer_name='$nickname',openid='$openid',faceimg='$faceimg',city='$city',province='$province',sex=$sex,last_time=now() where openid='" . $user_obj['openid'] . "'";
            DB::update($sql);
        } else {
            $sql = "insert into ht_customer (customer_group_id,customer_name,openid,faceimg,city,province,sex,create_time) values(1,'$nickname','$openid','$faceimg','$city','$province',$sex,now())";
            DB::insert($sql);
        }
        echo "<script>window.location.href='http://xuhuatang.com/huatang/public/ionshop'</script>";
//        echo "<script>window.location.href='http://localhost/huatang/public/ionshop'</script>";
    }

    public function getWeiUserInfo()
    {
        if (isset($_SESSION['user'])) {
            return response()->json(['error' => 'no', 'data' => $_SESSION['user']]);
        } else {
            return response()->json(['error' => 'yes', 'data' => '']);
        }
    }

    private function wei()
    {
        $appid = $this->appid;
        $secret = $this->secret;
        $code = $_GET["code"];
        $get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_token_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        $json_obj = json_decode($res, true);
        //根据openid和access_token查询用户信息
        $access_token = $json_obj['access_token'];
        $openid = $json_obj['openid'];
        $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $get_user_info_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        $res = curl_exec($ch);
        curl_close($ch);
        //解析json
        $user_obj = json_decode($res, true);
        return $user_obj;
    }

}
